<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>抽奖设置</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="css/setting/index.css">
	<link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all" />
</head>
<style>
	.item-price ul li {
		width: 100%;
		margin: 10px 10px;
		float: left;
	}
</style>
<body class="childrenBody">
<div class="layui-tab" lay-filter="prize">
	<ul class="layui-tab-title">
		<li class="layui-this">基础设置</li>
		<li>派奖方式</li>
		<li>奖项设置</li>

	</ul>
	<div class="layui-tab-content">

		<div class="layui-tab-item layui-show">
			<form class="layui-form" action="">
				<div class="item-price">
					<ul>
						<li>
							<p class="item-left">活动标题:</p>
							<div class="item-content item-other" style="height:auto">
								<input type="text" class="layui-input" lay-verify="required" name="luckyDrawTitle" placeholder="请输入活动标题">
							</div>
						</li>
						<li>
							<p class="item-left">活动时间:</p>
							<div class="item-content item-other" style="height:auto">
								<input name="startTime" id="test1" type="text" lay-verify="required" placeholder="请输入活动开始时间" class="layui-input">
								<input name="endTime" id="test2" type="text" lay-verify="required" placeholder="请输入活动结束时间" class="layui-input">
							</div>
						</li>
						<li>
							<p class="item-left">抽奖规则：</p>
							<div class="item-content item-other">
								<textarea style="height: 200px;" name="luckyDrawRule" placeholder="请输入抽奖规则" lay-verify="required" class="layui-textarea"></textarea>
							</div>
						</li>
						<li>
							<p style="color: red;">友情提示：活动期间不允许修改</p>
						</li>
						<li>
							<button class="layui-btn" lay-submit lay-filter="prizeForm">立即提交</button>
						</li>
					</ul>
				</div>
			</form>
		</div>
		<div class="layui-tab-item">
			<form class="layui-form" action="">
				<div class="item-price">
					<ul>
						<div>
							<p class="item-left" style="color: #DBDBDB;font-size:16px;">抽奖限制</p>
						</div>
						<li>
							<p class="item-left"  style="width: 100px; margin-top: 8px;">总抽奖机会:</p>
							<div class="item-content item-other" style="height:auto">
								<input type="radio" lay-filter="luckyDrawLimit" name="luckyDrawLimit" value="1" title="不限">
								<input type="radio" lay-filter="luckyDrawLimit" name="luckyDrawLimit" value="2" title="限制" >
								<input type="text" class="layui-input" lay-verify="required||number" name="overallLuckyDraw" placeholder="请输入每人抽奖总机会次数">
							</div>
						</li>
						<li >
							<p class="item-left" style="width: 100px;">每人抽奖机会:</p>
							<div class="item-content item-other" style="height:auto">
								<input name="luckyDrawDay" type="text" lay-verify="required||number" placeholder="请输入每人抽奖机会" class="layui-input">
							</div>
						</li>

						<div>
							<p class="item-left" style="color: #DBDBDB;font-size:16px;">中奖几率:</p>
						</div>
						<!--<li >-->
						<!--<p class="item-left" style="width: 100px;">每人中奖次数:</p>-->
						<!--<div class="item-content item-other" style="height:auto">-->
						<!--<input name="number" type="text" lay-verify="required||number" placeholder="请输入每人可中奖次数" class="layui-input">-->
						<!--</div>-->
						<!--</li>-->
						<li>
							<p class="item-left" style="width: 100px;">总中奖率:</p>
							<div class="item-content item-other" style="height:auto">
								<input name="percentage" type="text" lay-verify="required||number" placeholder="请输入总中奖率" class="layui-input">
								<p style="z-index: 9999; position: fixed ! important; right: 1260px; top: 240px;">%</p>
								<samp>例:设置为10,则意味着10次抽奖1次中奖</samp>
							</div>
						</li>
						<li>
							<p style="color: red;">友情提示：活动期间不允许修改</p>
						</li>
						<li>
							<button class="layui-btn" lay-submit lay-filter="awardForm">立即提交</button>
						</li>
					</ul>
				</div>
			</form>
		</div>
		<div class="layui-tab-item">
			<blockquote class="layui-elem-quote news_search">
				<form class="layui-form">
					<div class="layui-inline dynamicShow">
						<a class="layui-btn layui-btn-normal itemAdd_btn">添加奖项</a>
					</div>
				</form>
			</blockquote>
			<table class="layui-table" id="prizeItemList" lay-filter="prizeItemList"></table>
		</div>
	</div>
</div>
<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="plugins/layui/layui.all.js"></script>
<script type="text/javascript" src="js/user/newsList.js"></script>
<script src="js/TimerButton.js"></script>
<script type="text/html" id="barDemo">
	{{# if(d.prizeName!='未中奖'){ }}
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon"></i>删除</a>
	{{# } }}
</script>
<script type="text/javascript">
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
    Date.prototype.addDays = function (d) {
        this.setDate(this.getDate() + d);
    };
    Date.prototype.addWeeks = function (w) {
        this.addDays(w * 7);
    };
    Date.prototype.addMonths = function (m) {
        var d = this.getDate();
        this.setMonth(this.getMonth() + m);
        if (this.getDate() < d)
            this.setDate(0);
    };
    Date.prototype.addYears = function (y) {
        var m = this.getMonth();
        this.setFullYear(this.getFullYear() + y);
        if (m < this.getMonth()) {
            this.setDate(0);
        }
    };
    layui.config({
        base : "/js/admin/"
    }).extend({
        $tool: 'tool'
    }).use(['table','$tool','form','element','laydate'],function () {
        var table=layui.table,
            $tool=layui.$tool,
            laydate = layui.laydate,
            form=layui.form;
        var element = layui.element;
        element.on('tab(prize)', function(data){
            if (data.index==0){
                initThreshold();
            } else if(data.index==1){
                awardThreshold();
            }else if(data.index==2){//奖项设置
                prizeItem(table);
            }
        });
        laydate.render({
            elem: '#test1', //指定元素
            min:0, type: 'datetime',
            change:
                function(value, date, endDate){
                }
        });laydate.render({
            elem: '#test2', //指定元素
            min:0, type: 'datetime',
            change:
                function(value, date, endDate){
                }
        });
        initThreshold();//初始化
        function initThreshold() {
            $.ajax({
                type : "GET",
                url: '/luckydraw',
                dataType:'json',
                async:false,
                success: function(res){
                    if (res.data!=null){
                        $("input[name='luckyDrawTitle']").val(res.data.luckyDrawTitle);
                        $("textarea[name='luckyDrawRule']").text(res.data.luckyDrawRule);
                        $("input[name='startTime']").val($tool.formatTime1(res.data.startTime));
                        $("input[name='endTime']").val($tool.formatTime1(res.data.endTime));
                    }
                }
            });
        }
        form.on("submit(prizeForm)",function(data){
            var datas=data.field;
            var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            $.ajax({
                type: "POST",
                url:"/luckydraw/addOrUpdate",
                data:JSON.stringify(datas),// 序列化表单值
                //   dataType: "json",
                contentType:"application/json",
                success: function(res) {
                    if(res.code==2000){
                        top.layer.close(index);
                        $(".item-edit").css("display","none")
                        top.layer.msg("保存成功！");
                        initThreshold()
                    }else{
                        top.layer.msg(res.msg);
                    }
                } ,
                error: function(request) {
                    alert("请求错误");
                }
            });
            return false;
        });
        form.on("submit(awardForm)",function(data){
            var datas=data.field;
            var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            $.ajax({
                type: "POST",
                url:"/luckydraw/addOrUpdate",
                data:JSON.stringify(datas),// 序列化表单值
                //   dataType: "json",
                contentType:"application/json",
                success: function(res) {
                    if(res.code==2000){
                        top.layer.close(index);
                        $(".item-edit").css("display","none")
                        top.layer.msg("保存成功！");
                        awardThreshold()
                    }else{
                        top.layer.msg(res.msg);
                    }
                } ,
                error: function(request) {
                    alert("请求错误");
                }
            });
            return false;
        });
        function awardThreshold() {
            $.ajax({
                type : "GET",
                url: '/luckydraw',
                dataType:'json',
                async:false,
                success: function(res){
                    var luckyDrawLimit =2;
                    if (res.data!=null){
                        luckyDrawLimit = res.data.luckyDrawLimit
                        if (luckyDrawLimit==1){
                            $("input[name='overallLuckyDraw']").hide();
                            $("input[name='overallLuckyDraw']").removeAttr("lay-verify");
                        }else{
                            $("input[name='overallLuckyDraw']").show();
                            $("input[name='overallLuckyDraw']").attr("lay-verify","required||number");
                            $("input[name='overallLuckyDraw']").val(res.data.overallLuckyDraw);
                        }
                        $("input[name='percentage']").val(res.data.percentage);
                        $("input[name='luckyDrawDay']").val(res.data.luckyDrawDay);
                        $("input[name='number']").val(res.data.number);
                    }
                    $(":radio[name='luckyDrawLimit']").each(function(i,item){
                        if($(item).val()==luckyDrawLimit){
                            $(item).prop('checked',true);
                            form.render();
                        }
                    });
                }
            });
        }
        form.on('radio(luckyDrawLimit)', function(data){
            if (data.value==1){
                $("input[name='overallLuckyDraw']").hide();
                $("input[name='overallLuckyDraw']").removeAttr("lay-verify");
            } else{
                $("input[name='overallLuckyDraw']").show();
                $("input[name='overallLuckyDraw']").attr("lay-verify","required||number");
            }
        });
        $(".itemAdd_btn").click(function(){
            var index = layui.layer.open({
                title : "添加奖项",
                area:["100%","100%"],
                type : 2,
                content : "prizeitem/prizeitemAdd.html"
            })
        });
        table.on('tool(prizeItemList)', function(obj){
            var layEvent = obj.event;
            var ids=obj.data.id;
            if(layEvent === 'del'){ //删除
                layer.confirm('真的删除该奖项吗', function(index){
                    layer.close(index);
                    $.ajax({
                        type: "DELETE",
                        url:"luckydraw/delete/"+ids,
                        success: function(res) {
                            if(res.code==2000){
                                obj.del();
                                top.layer.close(index);
                                top.layer.msg("删除成功");
                                prizeItem(table);
                            }else{
                                top.layer.msg(res.msg);
                            }
                        } ,
                        error: function(res) {
                            layer.msg("请求错误");
                        }
                    });
                });
            }
        });
        function prizeItem(table) {
            var tableIns=table.render({
                elem: '#prizeItemList'
                ,url: '/luckydraw/prizeItemList'
                ,cols: [[
                    {field:'prizeName', title: '奖项名称'}
                    ,{field:'prizeCount', title: '奖项数量'}
                    ,{field:'prizeType', title: '奖项类型', templet:
                            function(d){
                                if (d.prizeName!='未中奖'){
                                    if (d.prizeType==1){
                                        return "优惠卷";
                                    } else if (d.prizeType==2) {
                                        return "卡卷";
                                    }else{
                                        return "实体";
                                    }
                                }
                                return "";
                            }
                    },{field:'name', title: '卡卷名称', templet:
                            function(d){
                                if (d.prizeName!='未中奖'){
                                    if (d.prizeType==1){
                                        return d.name+"("+d.amount+")元";
                                    } else if (d.prizeType==2) {
                                        return d.productName+"-"+d.categoryName+"("+d.productMoney+"元)";
                                    }else{
                                        return d.modelName;
                                    }
                                }
                                return "";
                            }
                    }
                    ,{field:'operation',title: '操作', align:'center', toolbar: '#barDemo',width:"25%"}
                ]]
                ,done : function(res, curr, count){
                    if(count>=8){
                        $(".itemAdd_btn").hide();
                    }else{
                        $(".itemAdd_btn").show();
                    }
                }
                ,id: 'testReload'
                // ,page: true
            });
        }


    });


</script>
</body>
</html>